
function addFile(caseId, fieldId){
    var table = document.getElementById(caseId + "_" + fieldId);
    var count = 0;
    if(table.rows.length > 0){
        count = parseInt(table.rows[table.rows.length - 1].id) + 1;
    }
    var e = document.createElement("input");
    e.setAttribute("type","file");
    e.setAttribute("id","file"+ caseId + "_" + fieldId + "_" + count);
    e.setAttribute("name","file");
    e.setAttribute("style","display:none");
    e.setAttribute("onchange","changeFile("+count+","+caseId+","+fieldId+")");
    document.getElementById("fieldAttachWindow").appendChild(e);
    e.click();
}

function changeFile(count, caseId, fieldId){
    var e = document.getElementById("file"+ caseId + "_" + fieldId + "_" + count);
    var filepath = e.value;
    filepath = filepath.substring(filepath.lastIndexOf("\\")+1);
    var table = document.getElementById(caseId + "_" + fieldId);
    var i = table.rows.length;
    var tr = document.createElement("tr");
    tr.setAttribute("id",count);
    var td1 = document.createElement("td");
    td1.setAttribute("width","70%");
    var td2 = document.createElement("td");
    td2.setAttribute("width","30%");
    td1.innerHTML = filepath;
    td2.innerHTML = "<a href=\"#\" onclick=\"deleteAttach("+i+","+caseId+","+fieldId+")\">delete</a>";
    tr.appendChild(td1);
    tr.appendChild(td2);
    table.appendChild(tr);
}

function attachPre(caseId, fieldId){
    var windowObj = document.getElementById("fieldAttachWindow");
    var fileObj = windowObj.getElementsByTagName("input");
    for(var j=0;j<fileObj.length;j++){
        windowObj.removeChild(fileObj[j]);
    }
    var e = document.getElementById("AttachAdd");
    e.setAttribute("onclick","addFile("+caseId+","+fieldId+")");
    var save = document.getElementById("AttachSave");
    save.setAttribute("onclick","saveFile("+caseId+","+fieldId+")");
    var tables = document.getElementsByName("AttachTable");
    var table = tables[0];
    table.setAttribute("id",caseId+"_"+fieldId);
    table.innerHTML = "";
    params = {};
    params['caseId'] = caseId;
    params['fieldId'] = fieldId;
    var attaches = "";
    $.ajax({
        url:"/CooperateWeb/table/getValue",
        type:'post',
        data:params,
        success:function(result){
            if(result.successful){
                attaches = result.message;
                var attachArr = attaches.split(";");
                for(var i=1;i<attachArr.length;i++){
                    var tr = document.createElement("tr");
                    tr.setAttribute("id",i);
                    var td1 = document.createElement("td");
                    var td2 = document.createElement("td");
                    td1.innerHTML = attachArr[i].substring(attachArr[i].lastIndexOf("\\")+1);
                    td2.innerHTML = "<a href=\"#\" onclick=\"deleteAttach("+(i-1)+","+caseId+","+fieldId+")\">delete</a>";
                    tr.appendChild(td1);
                    tr.appendChild(td2);
                    table.appendChild(tr);
                }
            }
        }
    });
}

function saveFile(caseId, fieldId){
    var ids = [];
    var ids_index = 0;
    var table = document.getElementById(caseId + "_" + fieldId);
    var value = "";
    for(var i=0;i<table.rows.length;i++){
        value += ";" + table.rows[i].cells[0].innerHTML;
    }
    for(var count=0;count<20;count++){
        var e = document.getElementById("file"+ caseId + "_" + fieldId + "_" + count);
        if(e!=null){
            ids[ids_index++] = e.id;
        }
    }
    params = {};
    params['caseId'] = caseId;
    params['fieldId'] = fieldId;
    params['value'] = value;
    $.ajax({
        url:"/CooperateWeb/table/update",
        type:'post',
        data:params,
        success:function(result){
            if(result.successful){
                var value_show = value;
                if(value.length>10){
                    value_show = value.substring(1,10) + "...";
                }
                $('#case_data').treegrid('update',{
                    id:fieldId,
                    row:{
                        data_value:value_show
                    }
                });
                $('#fieldAttachWindow').window('close');
            }

        }
    });
    $.ajaxFileUpload({
        url:"/CooperateWeb/upload/uploadFile",
        secureuri:false,
        fileElementId:ids,
        dataType:'json',
        success:function(data,status){
            if(data){
                //$.messager.alert("提示",data.result,"info");
            }else{
                $.messager.alert("提示",data.result,"error");
            }
        },
        error:function(data,status,e){

        }

    });
}

function deleteAttach(i,caseId,fieldId){
    var table = document.getElementById(caseId + "_" + fieldId);
    var rows = table.rows.length;
    for(var row=i+1;row<rows;row++){
        table.rows[row].getElementsByTagName("a")[0].setAttribute("onclick","deleteAttach("+(row-1)+","+caseId+","+fieldId+")");
    }
    var file_id = table.rows[i].id;
    table.deleteRow(i);
    var e = document.getElementById("file"+ caseId + "_" + fieldId + "_" + file_id);
    var windowObj = document.getElementById("fieldAttachWindow");
    if(e!=null)
        windowObj.removeChild(e);
    var value = "";
    for(var j=0;j<table.rows.length;j++){
        value += ";" + table.rows[j].cells[0].innerHTML;
    }
    params = {};
    params['caseId'] = caseId;
    params['fieldId'] = fieldId;
    params['value'] = value;
    $.ajax({
        url:"/CooperateWeb/table/update",
        type:'post',
        data:params,
        success:function(result){
            if(result.successful){

            }

        }
    });

}

function picPre(caseId, fieldId){
    var windowObj = document.getElementById("fieldPicWindow");
    var fileObj = windowObj.getElementsByTagName("input");
    for(var j=0;fileObj!=null&&j<fileObj.length;j++){
        windowObj.removeChild(fileObj[j]);
    }
    var e = document.getElementById("PicAdd");
    e.setAttribute("onclick","addPic("+caseId+","+fieldId+")");
    var save = document.getElementById("PicSave");
    save.setAttribute("onclick","savePic("+caseId+","+fieldId+")");
    var tables = document.getElementsByName("PicTable");
    var table = tables[0];
    table.setAttribute("id",caseId+"_"+fieldId);
    table.innerHTML = "";
    params = {};
    params['caseId'] = caseId;
    params['fieldId'] = fieldId;
    var attaches = "";
    $.ajax({
        url:"/CooperateWeb/table/getValue",
        type:'post',
        data:params,
        success:function(result){
            if(result.successful){
                attaches = result.message;
                var attachArr = attaches.split(";");
                for(var i=1;i<attachArr.length;i++){
                    var tr = document.createElement("tr");
                    tr.setAttribute("id",i);
                    var td1 = document.createElement("td");
                    var td2 = document.createElement("td");
                    td1.innerHTML = attachArr[i].substring(attachArr[i].lastIndexOf("\\")+1);
                    td2.innerHTML = "<a href=\"#\" onclick=\"deletePic("+(i-1)+","+caseId+","+fieldId+")\">delete</a>";
                    tr.appendChild(td1);
                    tr.appendChild(td2);
                    table.appendChild(tr);
                }
            }
        }
    });
}

function addPic(caseId, fieldId){
    var table = document.getElementById(caseId + "_" + fieldId);
    var count = 0;
    if(table.rows.length > 0){
        count = parseInt(table.rows[table.rows.length - 1].id) + 1;
    }
    var e = document.createElement("input");
    e.setAttribute("type","file");
    e.setAttribute("id","file"+ caseId + "_" + fieldId + "_" + count);
    e.setAttribute("name","file");
    e.setAttribute("style","display:none");
    e.setAttribute("accept","image/*");
    e.setAttribute("onchange","changePic("+count+","+caseId+","+fieldId+")");
    document.getElementById("fieldPicWindow").appendChild(e);
    e.click();
}

function changePic(count, caseId, fieldId){
    var e = document.getElementById("file"+ caseId + "_" + fieldId + "_" + count);
    var filepath = e.value;
    //var windowObj = document.getElementById("fieldPicWindow");
    //windowObj.removeChild(e);
    filepath = filepath.substring(filepath.lastIndexOf("\\")+1);
    var table = document.getElementById(caseId + "_" + fieldId);
    var i = table.rows.length;
    var tr = document.createElement("tr");
    tr.setAttribute("id", count);
    var td1 = document.createElement("td");
    td1.setAttribute("width","70%");
    var td2 = document.createElement("td");
    td2.setAttribute("width","30%");
    td1.innerHTML = filepath;
    td2.innerHTML = "<a href=\"#\" onclick=\"deletePic("+i+","+caseId+","+fieldId+")\">delete</a>";
    tr.appendChild(td1);
    tr.appendChild(td2);
    table.appendChild(tr);
}

function savePic(caseId, fieldId){
    var ids = [];
    var ids_index = 0;
    var table = document.getElementById(caseId + "_" + fieldId);
    var value = "";
    for(var i=0;i<table.rows.length;i++){
        value += ";" + table.rows[i].cells[0].innerHTML;
    }
    for(var count=0;count<20;count++){
        var e = document.getElementById("file"+ caseId + "_" + fieldId + "_" + count);
        if(e!=null){
            ids[ids_index++] = e.id;
        }
    }
    params = {};
    params['caseId'] = caseId;
    params['fieldId'] = fieldId;
    params['value'] = value;
    $.ajax({
        url:"/CooperateWeb/table/update",
        type:'post',
        data:params,
        success:function(result){
            if(result.successful){
                var value_show = value;
                if(value.length>10){
                    value_show = value.substring(1,10) + "...";
                }
                $('#case_data').treegrid('update',{
                    id:fieldId,
                    row:{
                        data_value:value_show
                    }
                });
                $('#fieldPicWindow').window('close');
            }

        }
    });
    $.ajaxFileUpload({
        url:"/CooperateWeb/upload/uploadPic",
        secureuri:false,
        fileElementId:ids,
        dataType:'json',
        success:function(data,status){
            if(data){
                //$.messager.alert("提示",data.result,"info");
            }else{
                $.messager.alert("提示",data.result,"error");
            }
        },
        error:function(data,status,e){

        }

    });
}

function deletePic(i,caseId,fieldId){
    var table = document.getElementById(caseId + "_" + fieldId);
    var rows = table.rows.length;
    for(var row=i+1;row<rows;row++){
        //table.rows[row].setAttribute("id",row-1);
        table.rows[row].getElementsByTagName("a")[0].setAttribute("onclick","deletePic("+(row-1)+","+caseId+","+fieldId+")");
    }
    var file_id = table.rows[i].id;
    table.deleteRow(i);
    var e = document.getElementById("file"+ caseId + "_" + fieldId + "_" + file_id);
    var windowObj = document.getElementById("fieldPicWindow");
    if(e!=null)
        windowObj.removeChild(e);
    var value = "";
    for(var j=0;j<table.rows.length;j++){
        value += ";" + table.rows[j].cells[0].innerHTML;
    }
    params = {};
    params['caseId'] = caseId;
    params['fieldId'] = fieldId;
    params['value'] = value;
    $.ajax({
        url:"/CooperateWeb/table/update",
        type:'post',
        data:params,
        success:function(result){
            if(result.successful){

            }

        }
    });

}

function threeDPre(caseId, fieldId){
    var windowObj = document.getElementById("field3DWindow");
    var fileObj = windowObj.getElementsByTagName("input");
    for(var j=0;j<fileObj.length;j++){
        windowObj.removeChild(fileObj[j]);
    }
    var e = document.getElementById("ThreeDAdd");
    e.setAttribute("onclick","add3D("+caseId+","+fieldId+")");
    var save = document.getElementById("ThreeDSave");
    save.setAttribute("onclick","save3D("+caseId+","+fieldId+")");
    var tables = document.getElementsByName("ThreeDTable");
    var table = tables[0];
    table.setAttribute("id",caseId+"_"+fieldId);
    table.innerHTML = "";
    params = {};
    params['caseId'] = caseId;
    params['fieldId'] = fieldId;
    var attaches = "";
    $.ajax({
        url:"/CooperateWeb/table/getValue",
        type:'post',
        data:params,
        success:function(result){
            if(result.successful){
                attaches = result.message;
                var attachArr = attaches.split(";");
                for(var i=1;i<attachArr.length;i++){
                    var tr = document.createElement("tr");
                    tr.setAttribute("id",i);
                    var td1 = document.createElement("td");
                    var td2 = document.createElement("td");
                    td1.innerHTML = attachArr[i].substring(attachArr[i].lastIndexOf("\\")+1);
                    td2.innerHTML = "<a href=\"#\" onclick=\"delete3D("+(i-1)+","+caseId+","+fieldId+")\">delete</a>";
                    tr.appendChild(td1);
                    tr.appendChild(td2);
                    table.appendChild(tr);
                }
            }
        }
    });
}

function add3D(caseId, fieldId){
    var table = document.getElementById(caseId + "_" + fieldId);
    var count = 0;
    if(table.rows.length > 0){
        count = parseInt(table.rows[table.rows.length - 1].id) + 1;
    }
    var e = document.createElement("input");
    e.setAttribute("type","file");
    e.setAttribute("id","file"+ caseId + "_" + fieldId + "_" + count);
    e.setAttribute("name","file");
    e.setAttribute("style","display:none");
    e.setAttribute("onchange","change3D("+count+","+caseId+","+fieldId+")");
    document.getElementById("field3DWindow").appendChild(e);
    e.click();
}

function change3D(count, caseId, fieldId){
    var e = document.getElementById("file"+ caseId + "_" + fieldId + "_" + count);
    var filepath = e.value;
    filepath = filepath.substring(filepath.lastIndexOf("\\")+1);
    var table = document.getElementById(caseId + "_" + fieldId);
    var i = table.rows.length;
    var tr = document.createElement("tr");
    tr.setAttribute("id", count);
    var td1 = document.createElement("td");
    td1.setAttribute("width","70%");
    var td2 = document.createElement("td");
    td2.setAttribute("width","30%");
    td1.innerHTML = filepath;
    td2.innerHTML = "<a href=\"#\" onclick=\"delete3D("+i+","+caseId+","+fieldId+")\">delete</a>";
    tr.appendChild(td1);
    tr.appendChild(td2);
    table.appendChild(tr);
}

function save3D(caseId, fieldId){
    var ids = [];
    var ids_index = 0;
    var table = document.getElementById(caseId + "_" + fieldId);
    var value = "";
    for(var i=0;i<table.rows.length;i++){
        value += ";" + table.rows[i].cells[0].innerHTML;
    }
    for(var count=0;count<20;count++){
        var e = document.getElementById("file"+ caseId + "_" + fieldId + "_" + count);
        if(e!=null){
            ids[ids_index++] = e.id;
        }
    }
    params = {};
    params['caseId'] = caseId;
    params['fieldId'] = fieldId;
    params['value'] = value;
    $.ajax({
        url:"/CooperateWeb/table/update",
        type:'post',
        data:params,
        success:function(result){
            if(result.successful){
                var value_show = value;
                if(value.length>10){
                    value_show = value.substring(1,10) + "...";
                }
                $('#case_data').treegrid('update',{
                    id:fieldId,
                    row:{
                        data_value:value_show
                    }
                });
                $('#field3DWindow').window('close');
            }

        }
    });
    $.ajaxFileUpload({
        url:"/CooperateWeb/upload/upload3D",
        secureuri:false,
        fileElementId:ids,
        dataType:'json',
        success:function(data,status){
            if(data){
                //$.messager.alert("提示",data.result,"info");
            }else{
                $.messager.alert("提示",data.result,"error");
            }
        },
        error:function(data,status,e){

        }

    });
}

function delete3D(i,caseId,fieldId){
    var table = document.getElementById(caseId + "_" + fieldId);
    var rows = table.rows.length;
    for(var row=i+1;row<rows;row++){
        table.rows[row].getElementsByTagName("a")[0].setAttribute("onclick","delete3D("+(row-1)+","+caseId+","+fieldId+")");
    }
    var file_id = table.rows[i].id;
    table.deleteRow(i);
    var e = document.getElementById("file"+ caseId + "_" + fieldId + "_" + file_id);
    var windowObj = document.getElementById("field3DWindow");
    if(e!=null)
        windowObj.removeChild(e);
    var value = "";
    for(var j=0;j<table.rows.length;j++){
        value += ";" + table.rows[j].cells[0].innerHTML;
    }
    params = {};
    params['caseId'] = caseId;
    params['fieldId'] = fieldId;
    params['value'] = value;
    $.ajax({
        url:"/CooperateWeb/table/update",
        type:'post',
        data:params,
        success:function(result){
            if(result.successful){

            }

        }
    });

}

function videoPre(caseId, fieldId){
    var windowObj = document.getElementById("fieldVideoWindow");
    var fileObj = windowObj.getElementsByTagName("input");
    for(var j=0;j<fileObj.length;j++){
        windowObj.removeChild(fileObj[j]);
    }
    var e = document.getElementById("VideoAdd");
    e.setAttribute("onclick","addVideo("+caseId+","+fieldId+")");
    var save = document.getElementById("VideoSave");
    save.setAttribute("onclick","saveVideo("+caseId+","+fieldId+")");
    var tables = document.getElementsByName("VideoTable");
    var table = tables[0];
    table.setAttribute("id",caseId+"_"+fieldId);
    table.innerHTML = "";
    params = {};
    params['caseId'] = caseId;
    params['fieldId'] = fieldId;
    var attaches = "";
    $.ajax({
        url:"/CooperateWeb/table/getValue",
        type:'post',
        data:params,
        success:function(result){
            if(result.successful){
                attaches = result.message;
                var attachArr = attaches.split(";");
                for(var i=1;i<attachArr.length;i++){
                    var tr = document.createElement("tr");
                    tr.setAttribute("id",i);
                    var td1 = document.createElement("td");
                    var td2 = document.createElement("td");
                    td1.innerHTML = attachArr[i].substring(attachArr[i].lastIndexOf("\\")+1);
                    td2.innerHTML = "<a href=\"#\" onclick=\"deleteVideo("+(i-1)+","+caseId+","+fieldId+")\">delete</a>";
                    tr.appendChild(td1);
                    tr.appendChild(td2);
                    table.appendChild(tr);
                }
            }
        }
    });
}

function addVideo(caseId, fieldId){
    var table = document.getElementById(caseId + "_" + fieldId);
    var count = 0;
    if(table.rows.length > 0){
        count = parseInt(table.rows[table.rows.length - 1].id) + 1;
    }
    var e = document.createElement("input");
    e.setAttribute("type","file");
    e.setAttribute("id","file"+ caseId + "_" + fieldId + "_" + count);
    e.setAttribute("name","file");
    e.setAttribute("style","display:none");
    e.setAttribute("accept","video/*");
    e.setAttribute("onchange","changeVideo("+count+","+caseId+","+fieldId+")");
    document.getElementById("fieldVideoWindow").appendChild(e);
    e.click();
}

function changeVideo(count, caseId, fieldId){
    var e = document.getElementById("file"+ caseId + "_" + fieldId + "_" + count);
    var filepath = e.value;
    filepath = filepath.substring(filepath.lastIndexOf("\\")+1);
    var table = document.getElementById(caseId + "_" + fieldId);
    var i = table.rows.length;
    var tr = document.createElement("tr");
    tr.setAttribute("id", count);
    var td1 = document.createElement("td");
    td1.setAttribute("width","70%");
    var td2 = document.createElement("td");
    td2.setAttribute("width","30%");
    td1.innerHTML = filepath;
    td2.innerHTML = "<a href=\"#\" onclick=\"deleteVideo("+i+","+caseId+","+fieldId+")\">delete</a>";
    tr.appendChild(td1);
    tr.appendChild(td2);
    table.appendChild(tr);
}

function saveVideo(caseId, fieldId){
    var ids = [];
    var ids_index = 0;
    var table = document.getElementById(caseId + "_" + fieldId);
    var value = "";
    for(var i=0;i<table.rows.length;i++){
        value += ";" + table.rows[i].cells[0].innerHTML;
    }
    for(var count=0;count<20;count++){
        var e = document.getElementById("file"+ caseId + "_" + fieldId + "_" + count);
        if(e!=null){
            ids[ids_index++] = e.id;
        }
    }
    params = {};
    params['caseId'] = caseId;
    params['fieldId'] = fieldId;
    params['value'] = value;
    $.ajax({
        url:"/CooperateWeb/table/update",
        type:'post',
        data:params,
        success:function(result){
            if(result.successful){
                var value_show = value;
                if(value.length>10){
                    value_show = value.substring(1,10) + "...";
                }
                $('#case_data').treegrid('update',{
                    id:fieldId,
                    row:{
                        data_value:value_show
                    }
                });
                $('#fieldVideoWindow').window('close');
            }

        }
    });
    $.ajaxFileUpload({
        url:"/CooperateWeb/upload/uploadVideo",
        secureuri:false,
        fileElementId:ids,
        dataType:'json',
        success:function(data,status){
            if(data){
                //$.messager.alert("提示",data.result,"info");
            }else{
                $.messager.alert("提示",data.result,"error");
            }
        },
        error:function(data,status,e){

        }

    });
}

function deleteVideo(i,caseId,fieldId){
    var table = document.getElementById(caseId + "_" + fieldId);
    var rows = table.rows.length;
    for(var row=i+1;row<rows;row++){
        table.rows[row].getElementsByTagName("a")[0].setAttribute("onclick","deleteVideo("+(row-1)+","+caseId+","+fieldId+")");
    }
    var file_id = table.rows[i].id;
    table.deleteRow(i);
    var e = document.getElementById("file"+ caseId + "_" + fieldId + "_" + file_id);
    var windowObj = document.getElementById("fieldVideoWindow");
    if(e!=null)
        windowObj.removeChild(e);
    var value = "";
    for(var j=0;j<table.rows.length;j++){
        value += ";" + table.rows[j].cells[0].innerHTML;
    }
    params = {};
    params['caseId'] = caseId;
    params['fieldId'] = fieldId;
    params['value'] = value;
    $.ajax({
        url:"/CooperateWeb/table/update",
        type:'post',
        data:params,
        success:function(result){
            if(result.successful){

            }

        }
    });

}

function TextPre(caseId, fieldId){
    var params = {};
    params['caseId'] = caseId;
    params['fieldId'] = fieldId;
    $.ajax({
        url:"/CooperateWeb/table/getValue",
        type:'post',
        data:params,
        success:function(result){
            if(result.successful){
                var textbox = document.getElementById('multiTextBox');
                textbox.innerHTML = result.message;
                textbox.value = result.message;
            }
        }
    });
}
function MatrixPre(caseId, fieldId){
    var params = {};
    params['fieldId'] = fieldId;
    $.ajax({
        url:"/CooperateWeb/table/getMatrixHead",
        type:'post',
        data:params,
        success:function(result){
            if(result.successful){
                var head = result.message;
                var head_arr = head.split(";");
                var matrixTableHead = [];
                var column = [];
                column[0] = [];
                for(var i=0;i<head_arr.length;i++){
                    var head_and_unit = head_arr[i].split("|");
                    if(head_and_unit.length == 1){
                        matrixTableHead[i] = head_and_unit[0];
                       
                    }else{
                        matrixTableHead[i] = head_and_unit[0] + "（" + head_and_unit[1] + "）";                       
                    }
                    column[0][i] = {};
                    column[0][i]["field"] = matrixTableHead[i];
                    column[0][i]["title"] = matrixTableHead[i];
                    column[0][i]["width"] = 100;
                    column[0][i]['editor'] = 'text';                   
                }
                $("#matrix_data").datagrid({
                    columns:column             
                });
                params['caseId'] = caseId;
                $.ajax({
                    url:"/CooperateWeb/table/getValue",
                    type:'post',
                    data:params,
                    success:function(res){
                        if(res.successful){
                            var matrix_value = res.message;
                            var matrix_json = [];
                            var matrix_value_arr = matrix_value.split("#");
                            for(var index1=0;index1<matrix_value_arr.length;index1++){
                                matrix_json[index1] = {};
                                var matrix_value_td = matrix_value_arr[index1].split(";");
                                for(var index2=0;index2<matrix_value_td.length;index2++){
                                	//如果表格没有数值 则默认为空
                                	if(matrix_value_td[index2]=="undefined"){
                                		matrix_value_td[index2]=" ";
                                	}
                                    matrix_json[index1][matrixTableHead[index2]] = matrix_value_td[index2];
                                   // alert("matrix_value_td[index2]"+matrix_value_td[index2]);
                                }
                            }
                            $('#matrix_data').datagrid({
                                data:matrix_json
                            });
                        }
                    }
                });
            }

        }
    });
}

function LinkPre(caseId, fieldId){
    var params = {};
    params['caseId'] = caseId;
    params['fieldId'] = fieldId;
    $.ajax({
        url:"/CooperateWeb/table/getValue",
        type:'post',
        data:params,
        success:function(result){
            if(result.successful){
                var checkedIds = result.message;
                if(checkedIds==""||checkedIds==null||checkedIds=="null"||checkedIds==" "){
                    checkedIds = 0;
                }
                var url = "/CooperateWeb/case/caseLink?checkIds="+checkedIds;
                $.ajax({
                    url:url,
                    success:function(result){
                        if(result.successful){
                            var casedata = eval("("+result.message+")");
                            $('#case_tree').tree({
                                checkbox:true,
                                onlyLeafCheck: true,
                                animate:true,
                                lines:true,
                                data: casedata,
                                formatter: function(node) {
                                    return node.text + "-";
                                }
                            });
                            $('#caseLinkWindow').window('open');
                            var saveCaseLink = document.getElementById('caseLink_ok');
                            saveCaseLink.setAttribute("onclick","saveLink("+caseId+","+fieldId+")");
                        }
                    }
                });
            }
        }
    });
}

function saveLink(caseId, fieldId){
    var cases = $('#case_tree').tree('getChecked');
    var ids = "";
    for(var i=0;i<cases.length;i++){
        ids = ids + "," + cases[i].id;
    }
    var value = ids.substring(1);
    var params = {};
    params['caseId'] = caseId;
    params['fieldId'] = fieldId;
    params['value'] = value;
    $.ajax({
        url:"/CooperateWeb/table/update",
        type:'post',
        data:params,
        success:function(result){
            if(result.successful){
                var value_show = value;
                if(value.length>10){
                    value_show = value.substring(0,10) + "...";
                }
                $('#case_data').treegrid('update',{
                    id:fieldId,
                    row:{
                        data_value:value_show
                    }
                });
                $('#caseLinkWindow').window('close');
            }

        }
    });
}

function threeDMatrixPre(caseId, fieldId){
    // get matrixHead and data based on caseId and fieldId
    var params = {};
    params['fieldId'] = fieldId;
    $.ajax({
        url:"/CooperateWeb/table/get3MatrixZHead",
        type:'post',
        data:params,
        success:function(result){
            if(result.successful){
                var head = result.message;
                var head_arr = head.split("*");
                var head_x = head_arr[0];
                var head_y = head_arr[1];

                var head_y_arr = head_y.split("#");
                var y_arr = head_y_arr[1].split(";");
                var matrixTableHead = [];
                var column = [];
                column[0] = [];
                column[0][0] = {};
                column[0][0]["field"] = "target";
                column[0][0]["title"] = "      ";
                column[0][0]["width"] = 100;
                for(var i=1;i<=y_arr.length;i++){
                    matrixTableHead[i] = head_y_arr[0] + "（" + y_arr[i-1] + "）";
                    column[0][i] = {};
                    column[0][i]["field"] = matrixTableHead[i];
                    column[0][i]["title"] = matrixTableHead[i];
                    column[0][i]["width"] = 80;
                    column[0][i]['editor'] = 'text';
                }
                $("#threeDmatrix_data").datagrid({
                    columns:column
                });              
                var head_x_arr = head_x.split("#");
                var x_arr = head_x_arr[1].split(";");
                params['caseId'] = caseId;
                $.ajax({
                    url:"/CooperateWeb/table/get3MatrixZValue",
                    type:'post',
                    data:params,
                    success:function(res){
                        if(res.successful){
                            var matrix_value = res.message;
                            var matrix_json = [];
                            if(matrix_value==""){
                                for(var i=0;i<x_arr.length;i++){
                                    matrix_json[i] = {};
                                    matrix_json[i]["target"] = head_x_arr[0] + "（" + x_arr[i] + "）";
                                    for(var j=1;j<=y_arr.length;j++){
                                        matrix_json[i][matrixTableHead[j]] = " ";
                                    }
                                }
                            }else{
                                var matrix_value_arr = matrix_value.split("#");
                                for(var i=0;i<x_arr.length;i++){
                                    var matrix_row_arr = matrix_value_arr[i].split(";");
                                    matrix_json[i] = {};
                                    matrix_json[i]["target"] = head_x_arr[0] + "（" + x_arr[i] + "）";
                                    for(var j=1;j<=y_arr.length;j++){
                                    	//如果表格没有数值 则默认为空
                                    	if(matrix_row_arr[j-1]=="undefined"){
                                    		matrix_row_arr[j-1]=" ";
                                    	}
                                        matrix_json[i][matrixTableHead[j]] = matrix_row_arr[j-1];                                       
                                    }
                                }
                            }

                            $('#threeDmatrix_data').datagrid({
                                data:matrix_json
                            });
                        }
                    }
                });
            }

        }
    });
}

function threeDMatrixPre_X(caseId, fieldId){
    var params = {};
    params['fieldId'] = fieldId;
    $.ajax({
        url:"/CooperateWeb/table/get3MatrixXHead",
        type:'post',
        data:params,
        success:function(result){
            if(result.successful){
                var ms = result.message;
                var name = ms.split("#")[0];
                var head = ms.split("#")[1];
                var head_arr = head.split(";");

                var matrixTableHead = [];
                var column = [];
                column[0] = [];
                column[0][0] = {};
                column[0][0]["field"] = "target";
                column[0][0]["title"] = "      ";
                column[0][0]["width"] = 50;
                column[0][0]['editor'] = 'text';
                for(var i=1;i<=head_arr.length;i++){
                    matrixTableHead[i] = name + "（" + head_arr[i-1] + "）";
                    column[0][i] = {};
                    column[0][i]["field"] = matrixTableHead[i];
                    column[0][i]["title"] = matrixTableHead[i];
                    column[0][i]["width"] = 100;
                    column[0][i]['editor'] = 'text';
                }
                $("#threeDmatrix_data_X").datagrid({
                    columns:column
                });
                params['caseId'] = caseId;
                $.ajax({
                    url:"/CooperateWeb/table/get3MatrixXValue",
                    type:'post',
                    data:params,
                    success:function(res){
                        if(res.successful){
                            var matrix_value = res.message;
                            var matrix_json = [];
                            var matrix_value_arr = matrix_value.split("#");
                            for(var index1=0;index1<matrix_value_arr.length;index1++){
                                matrix_json[index1] = {};
                                var matrix_value_td = matrix_value_arr[index1].split(";");
                                //如果X编辑中X表头值为undefined时默认为空
                                if(matrix_value_td[0]=="undefined"){
                                	matrix_value_td[0]=" ";
                                }
                                matrix_json[index1]["target"] = matrix_value_td[0];
                                for(var index2=1;index2<matrix_value_td.length;index2++){
                                	//如果X编辑表格中为空时默认为空
                                	if(matrix_value_td[index2]=="undefined"){
                                		matrix_value_td[index2]=" ";
                                	}
                                    matrix_json[index1][matrixTableHead[index2]] = matrix_value_td[index2];
                                }
                            }
                            $('#threeDmatrix_data_X').datagrid({
                                data:matrix_json
                            });
                        }
                    }
                });
            }

        }
    });
}

function threeDMatrixPre_Y(caseId, fieldId){
    var params = {};
    params['fieldId'] = fieldId;
    $.ajax({
        url:"/CooperateWeb/table/get3MatrixYHead",
        type:'post',
        data:params,
        success:function(result){
            if(result.successful){
                var ms = result.message;
                var name = ms.split("#")[0];
                var head = ms.split("#")[1];
                var head_arr = head.split(";");

                var matrixTableHead = [];
                var column = [];
                column[0] = [];
                column[0][0] = {};
                column[0][0]["field"] = "target";
                column[0][0]["title"] = "      ";
                column[0][0]["width"] = 50;
                column[0][0]['editor'] = 'text';
                for(var i=1;i<=head_arr.length;i++){
                    matrixTableHead[i] = name + "（" + head_arr[i-1] + "）";
                    column[0][i] = {};
                    column[0][i]["field"] = matrixTableHead[i];
                    column[0][i]["title"] = matrixTableHead[i];
                    column[0][i]["width"] = 100;
                    column[0][i]['editor'] = 'text';
                }
                $("#threeDmatrix_data_Y").datagrid({
                    columns:column
                });
                params['caseId'] = caseId;
                $.ajax({
                    url:"/CooperateWeb/table/get3MatrixYValue",
                    type:'post',
                    data:params,
                    success:function(res){
                        if(res.successful){
                            var matrix_value = res.message;
                            var matrix_json = [];
                            var matrix_value_arr = matrix_value.split("#");
                            for(var index1=0;index1<matrix_value_arr.length;index1++){
                                matrix_json[index1] = {};
                                var matrix_value_td = matrix_value_arr[index1].split(";");
                                //如果Y编辑中Y的表头值为undefined时默认为空
                                if(matrix_value_td[0]=="undefined"){
                                	matrix_value_td[0]=" ";
                                }
                                matrix_json[index1]["target"] = matrix_value_td[0];
                                for(var index2=1;index2<matrix_value_td.length;index2++){
                                	//如果Y编辑表格中为undefined则默认为空
                                	if(matrix_value_td[index2]=="undefined"){
                                		matrix_value_td[index2]=" ";
                                	}
                                    matrix_json[index1][matrixTableHead[index2]] = matrix_value_td[index2];
                                }
                            }
                            $('#threeDmatrix_data_Y').datagrid({
                                data:matrix_json
                            });
                        }
                    }
                });
            }

        }
    });
}